package com.zhp.dao;

import com.sun.tools.javac.code.Attribute;
import com.zhp.common.Constant;
import com.zhp.dao.base.BaseDao;
import com.zhp.model.User;
import com.zhp.utils.JDBCUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao<User> {
    public UserDao() {
        super(User.class);
    }

    /**
     * 根据用户名关联查询用户的基本信息
     *
     * @param username 用户名
     * @return 返回map对象 有头像 部门名称等
     */
    public Map<String, Object> findBaseInfoByName(String username) {
        String sql = "select u.id as userId,u.username,ui.real_name as realName,ui.pic,d.`name` as deptName " +
                " from user u " +
                " LEFT join user_info as ui on u.id = ui.user_id " +
                " LEFT JOIN dept as d on  u.dept_id = d.id " +
                " where username = ? ";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> query = null;
        try {
            query = queryRunner.query(sql, new MapHandler(), username);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }


    public List<Map<String, Object>> findUsers(String userId, int currentPage, String name) {
        String sql = "select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,\n" +
                "(SELECT id from user_focus where user_id = ? and user_focus_id = ui.user_id) as fid\n" +
                "from user_info ui\n" +
                "limit ?,?";
        if (name != null) {
            sql = "select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,\n" +
                    "(SELECT id from user_focus where user_id = ? and user_focus_id = ui.user_id) as fid\n" +
                    "from user_info ui\n" +
                    "where ui.real_name like ?" +
                    "LIMIT ?,?";
        }
        List<Map<String, Object>> list = new ArrayList<>();
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null == name) {
                list = queryRunner.query(sql, new MapListHandler(), userId, (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
            } else {
                list = queryRunner.query(sql, new MapListHandler(), userId, "%" + name + "%", (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getUserTotal(String name) {
        long total = 0L;
        String sql = "select count(*) as totalUser from user_info";
        if (null != name) {
            sql = "select count(*) as totalUser from user_info where real_name like ?";
        }
        Map<String, Object> map = new HashMap<>();
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null == name) {
                map = queryRunner.query(sql, new MapHandler());
            } else {
                map = queryRunner.query(sql, new MapHandler(), "%" + name + "%");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (map != null) {
            total = Long.parseLong(map.get("totalUser").toString());
        }
        return total;

    }

    public List<User> findMyFocus(String id, int currentPage) {
        String sql = "SELECT * from user where id in (select uf.user_focus_id from user_focus as uf where user_id = " + id + ") LIMIT ?,?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            BasicRowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
            List<User> userList = queryRunner.query(sql, new BeanListHandler<>(User.class, processor), (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Number findMyFocusAll(String id) {
        String sql = "SELECT count(*) as totalCount  from user where id in (select uf.user_focus_id from user_focus as uf where user_id = " + id + ")";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            return queryRunner.query(sql, new ScalarHandler<Integer>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
